﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data.Linq
Imports System.Text
Imports DevComponents
Imports System.Data
Imports DevComponents.DotNetBar
Imports TNCDAL
Imports TNCBAL
Imports HTLibrary
Imports System.Xml

Public Class ucCurrencies

    Dim mcur As New MCurrencies
    Dim ecur As New tblCurrency ' ECurrencies
    Dim TotRowCount As Integer
    Dim RowId As Integer
    Dim isnew As Boolean = False
    Dim isedit As Boolean = False
    Dim IndexRow As Integer = 0
    'Write log
    Dim objLogController As New LogController
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(grid)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub bindingTogrid()
        Dim query = mcur.getCurrenciesList2()
        Dim source As New BindingSource
        source.DataSource = query
        BindingNavigator1.BindingSource = source
        grid.DataSource = source
        grid.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        grid.Columns(0).Visible = False
        Try
            grid.CurrentCell.Selected = False

        Catch ex As Exception

        End Try
    End Sub
    Sub bindingTogridSearch()
        Dim query = mcur.getCurrenciesList2(txtCurrCodeSearch.Text)
        Dim source As New BindingSource
        source.DataSource = query
        BindingNavigator1.BindingSource = source
        grid.DataSource = source
        grid.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        grid.Columns(0).Visible = False
    End Sub
    Private Sub EnableControls(ByVal v As Boolean)
        txtCode.Enabled = v
        txtName.Enabled = v
        btnSave.Enabled = v
        btnCancel.Enabled = v

        If isnew Then
            txtIDCurrency.Text = ""
            txtCode.Text = ""
            txtName.Text = ""
            txtCode.Focus()

        End If

    End Sub
    Private Sub frmCurrencies_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bindingTogrid()
    End Sub

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'ecur = New tblCurrency
        'If grid.Rows(RowId).Cells(1).FormattedValue.ToString() <> "" Then
        '    ecur.code = grid.Rows(RowId).Cells(1).Value.ToString()
        'Else : ecur.code = vbNull
        'End If
        'If grid.Rows(RowId).Cells(2).FormattedValue.ToString() <> "" Then
        '    ecur.name = grid.Rows(RowId).Cells(2).Value.ToString()
        'Else : ecur.name = ""
        'End If
        ''If grid.Rows(RowId).Cells(3).FormattedValue.ToString() <> "" Then
        ''    ecur.name2 = grid.Rows(RowId).Cells(3).Value.ToString()
        ''Else : ecur.name2 = ""
        ''End If
        '' ecur.ratio = Convert.ToInt32(grid.Rows(RowId).Cells(4).Value)
        ''If grid.Rows(RowId).Cells(5).FormattedValue.ToString() <> "" Then
        ''    ecur. = grid.Rows(RowId).Cells(5).Value.ToString()
        ''Else : ecur.name2 = ""
        ''End If
        'Try

        '    mcur.InsertNewRecord(ecur)
        '    bindingTogrid()



        'Catch ex As Exception
        '    Throw ex
        'End Try
        Try
            Dim obj As New tblCurrency



        Catch ex As Exception

        End Try
    End Sub


    Private Sub gridCurrency_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid.CellClick
        RowId = grid.CurrentRow.Index
        ecur.ID_Currency = Convert.ToInt32(grid(0, RowId).Value)
        TotRowCount = grid.RowCount - 1
        If (RowId = TotRowCount) Then
            'btnInsert.Visible = True
            ' btnDelete.Visible = True
            ' btnedit.Visible = True
        End If
    End Sub



    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If (MessageBox.Show("Do you really want to delete " & txtName.Text & " record?",
                       "Delete Warning", MessageBoxButtons.YesNo,
                       MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, False) = DialogResult.Yes) Then

            'For i As Integer = 0 To grid.SelectedRows.Count - 1
            '    RowId = grid.SelectedRows(i).Index
            '    ecur.ID_Currency = Convert.ToInt32(grid(0, RowId).Value)

            '    If ecur.ID_Currency <> 0 Then
            '        mcur.DeleteRecord(ecur.ID_Currency)
            '    End If

            'Next

            Dim id As Integer = CInt(txtIDCurrency.Text)

            If id > 0 Then
                mcur.DeleteUpdateRecord(id)
                bindingTogrid()
                EnableControls(False)
                btnAdd.Enabled = True
                btnEdit.Enabled = True
                btnDelete.Enabled = True
                btnSave.Enabled = False
                btnCancel.Enabled = False

                objLogController.Insert(enumAction.Delete, String.Format("Currency {0}", txtCode.Name))

            End If


        End If

        ' bindingTogrid()
        ' btnDelete.Visible = False
        ' btnInsert.Visible = False
        ' btnEdit.Visible = False
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        'grid.AllowUserToAddRows = True
        grid.Enabled = False
        isedit = False
        isnew = True
        EnableControls(True)
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True

    End Sub



    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If isnew Then
            'nếu thêm mới
            Dim obj As New tblCurrency
            If txtCode.Text = "" Then
                MessageBox.Show("Chưa nhập mã")
                Return
            End If
            obj.Code = txtCode.Text.Trim()

            If txtName.Text <> "" Then
                obj.Name = txtName.Text.Trim()
            End If
            obj.IsActive = True
            mcur.InsertNewRecord(obj)
            objLogController.Insert(enumAction.Add, String.Format("Currency ({0})", obj.Code))
            isnew = False
            isedit = False
            bindingTogrid()
            EnableControls(False)

        ElseIf isedit Then
            Dim obj As New tblCurrency
            obj.ID_Currency = CInt(txtIDCurrency.Text)
            If txtCode.Text = "" Then
                MessageBox.Show("Chưa nhập mã")
                Return
            End If
            obj.Code = txtCode.Text

            If txtName.Text <> "" Then
                obj.Name = txtName.Text
            End If

            mcur.UpdateRecord(obj)
            objLogController.Insert(enumAction.Edit, String.Format("Currency ({0})", obj.Code))
            isnew = False
            isedit = False
            bindingTogrid()
            EnableControls(False)
            grid.Rows(IndexRow).Selected = True
            grid.FirstDisplayedScrollingRowIndex = IndexRow
            grid.CurrentCell = grid(1, IndexRow)
        End If
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        grid.Enabled = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        isnew = False
        isedit = False
        EnableControls(False)
        bindingTogrid()
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        grid.Enabled = True
    End Sub

    Private Sub grid_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid.RowEnter
        Dim rw As Integer = e.RowIndex
        Dim dg As DataGridView = CType(sender, DataGridView)
        txtCode.Text = dg.Item(1, rw).Value.ToString.Trim()  'row.Item("id").ToString
        txtName.Text = dg.Item(2, rw).Value 'row.Item("caption").ToString
        txtIDCurrency.Text = dg.Item(0, rw).Value.ToString.Trim() 'row.Item("menu_desc").ToString

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'grid.ReadOnly = False
        isnew = False
        isedit = True
        EnableControls(True)
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        grid.Enabled = False
        txtCode.Focus()
        IndexRow = grid.CurrentRow.Index
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        bindingTogridSearch()
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        bindingTogrid()
    End Sub

    Private Sub txtCurrCodeSearch_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCurrCodeSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            bindingTogridSearch()
        End If
    End Sub
End Class